<template>
  <div>
    <el-card style="margin: 20px 0px" shadow="hover">
      <CategorySelect @getcategoryId="getcategoryId" :isShowTable="!this.isShowTable" />
    </el-card>
    <el-card shadow="hover">
      <!-- 商品属性 -->
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addAttr" style="margin:20px 0">添加属性</el-button>

        <el-table border style="width: 100%" :data="attrList" stripe>
          <el-table-column type="index" label="序号" width="150" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="250" align="center">
          </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="500" align="center">
            <template slot-scope="{ row }">
              <el-tag type="success" v-for="attrValue in row.attrValueList" :key="attrValue.id" style="margin: 0 15px">
                {{ attrValue.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width" align="center">
            <template slot-scope="{ row }">
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="updateAttr(row)">修改</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 修改商品属性 -->
      <div v-show="!isShowTable">
        <el-form ref="form" :inline="true" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input placeholder="请输入属性名" v-model="attrInfo.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" style="margin-left: 30px" @click="addAttrValue"
          :disabled="!attrInfo.attrName">添加属性值</el-button>
        <el-button type="info" @click="isShowTable = true" icon="el-icon-info">取消</el-button>
        <el-table style="width: 100%; margin: 20px 0px" border :data="attrInfo.attrValueList" stripe>
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column label="属性值名称" width="width" prop="prop">
            <template slot-scope="{ row, $index }">
              <el-input v-model="row.valueName" placeholder="请输入属性值" size="mini" v-if="row.flag"
                @blur="row.flag = false" @keyup.enter.native="row.flag = false" :ref="$index"></el-input>
              <span v-else @click="toEdit(row, $index)">{{
                  row.valueName
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width" prop="prop">
            <template slot-scope="{ row, $index }">
              <el-popconfirm :title="`确定要删除${row.valueName}属性值吗？`" @onConfirm="deleteAttrValue($index)">
                <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
        <el-button type="success" @click="addOrUpdateAttr" style="margin-left:30px;" icon="el-icon-folder"
          :disabled="attrInfo.attrValueList < 1">保存</el-button>
        <el-button type="info" @click="isShowTable = true" icon="el-icon-info">取消</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      attrList: [],
      isShowTable: true,
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    getcategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        //三级分类的id有了，可以发请求了
        this.category3Id = categoryId;
        this.getAttrList();
      }
    },
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this;
      let result = await this.$API.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (result.code == 200) {
        this.attrList = result.data;
      }
    },
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "",
        flag: true,
      }),
        this.$nextTick(() => {
          this.$refs[this.attrInfo.attrValueList.length - 1].focus();
        });
    },
    addAttr() {
      this.isShowTable = false;
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3,
      };
    },
    updateAttr(row) {
      this.isShowTable = false;
      this.attrInfo = cloneDeep(row);
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, "flag", false);
      });
    },
    toEdit(row, index) {
      row.flag = true;
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1);
    },
    async addOrUpdateAttr() {
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        if (item.valueName != '') {
          delete item.flag;
          return true;
        };
      })
      try {
        await this.$API.reqAddOrUpdateAttr(this.attrInfo);
        this.isShowTable = true
        this.$message({ type: 'success', message: '保存成功' });

        this.getAttrList();
      } catch (error) {
        alert(error.message)
      }
    },

  },
};
</script>

<style>
</style>